package cn.com.zero.manager.auth.controller;

import cn.com.zero.api.log.annotation.Log;
import cn.com.zero.api.message.dubbo.IMessageService;
import cn.com.zero.base.vo.ResponseVO;
import cn.com.zero.manager.auth.constant.LogType;
import cn.com.zero.manager.auth.service.IAuthService;
import cn.com.zero.web.controller.BaseController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.dubbo.config.annotation.DubboReference;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;


/**
 * @author guoxin
 * @version 1.0.0
 * @Description
 * @date 2023/2/13 14:45
 */
@Api(tags = "后台登录退出接口")
@Validated
@RestController
@RequestMapping("/admin")
public class AuthController extends BaseController {

    /**
     * 后台登录退出service
     */
    private IAuthService authService;

    @PostMapping("/login")
    @ApiOperation("使用账号密码登录")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "userName", value = "用户名"),
            @ApiImplicitParam(paramType = "query", name = "password", value = "密码"),
            @ApiImplicitParam(paramType = "query", name = "loginClient", value = "登录设备：1：pc-web 2：android 3：ios 4：mobile-web")
    })
    @Log(content = "账号密码登录",logType = LogType.LOG_MANAGE)
    public ResponseVO login(@RequestParam(value = "userName") String userName,
                            @RequestParam(value = "password") String password,
                            @RequestParam(value = "loginClient")
                                @Range(min = 1, max = 4, message = "登录设备标识为非法数字")Integer loginClient){
        // 登录参数 ：  用户名 密码 来源：（PC,WEB）  // 手机验证码登录  手机号，验证码 ，来源：  有一个新的发短信的接口，

        // 登录成功，需要返回当前用户的权限 给后台 页面

        return authService.login(userName,password,loginClient);
    }

    @PostMapping("/logout")
    @Log(content = "账号退出",logType = LogType.LOG_MANAGE)
    public ResponseVO logout(){
        return authService.logout();
    }

    // ========== 短信登录相关 ==========

    @PostMapping("/sms-login")
    @ApiOperation("使用手机号登录")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "mobile", value = "手机号"),
            @ApiImplicitParam(paramType = "query", name = "smsCode", value = "验证码"),
            @ApiImplicitParam(paramType = "query", name = "loginClient", value = "登录设备：1：pc-web 2：android 3：ios 4：mobile-web")
    })
    @Log(content = "手机号登录",logType = LogType.LOG_MANAGE)
    public ResponseVO smsLogin(@RequestParam(value = "mobile") String mobile,
                               @RequestParam(value = "smsCode") String smsCode,
                               @RequestParam(value = "loginClient")
                                   @Range(min = 1, max = 4, message = "登录设备标识为非法数字")Integer loginClient) {
       return authService.smsLogin(mobile,smsCode,loginClient);
    }

    @PostMapping("/send-sms-code")
    @ApiImplicitParam(paramType = "query", name = "mobile", value = "手机号")
    @ApiOperation(value = "发送手机验证码")
    @Log(content = "发送短信验证码",logType = LogType.LOG_MANAGE)
    public void sendLoginSmsCode(@RequestParam(value = "mobile") String mobile) {

        authService.sendLoginSmsCode(mobile);
    }

    @Autowired
    public void setAuthService(IAuthService authService){
        this.authService = authService;
    }
}
